package com.example.springbootServiceNetwork.demos.web.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springbootServiceNetwork.demos.web.Config.JwtInfo;
import com.example.springbootServiceNetwork.demos.web.Config.Result;
import com.example.springbootServiceNetwork.demos.web.Config.ResultEnum;
import com.example.springbootServiceNetwork.demos.web.Dao.GoodsDao;
import com.example.springbootServiceNetwork.demos.web.Dao.MerchantDao;
import com.example.springbootServiceNetwork.demos.web.Entity.Goods;
import com.example.springbootServiceNetwork.demos.web.Entity.Merchant;
import com.example.springbootServiceNetwork.demos.web.Service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;

import static com.example.springbootServiceNetwork.demos.web.Tool.SerialNumberTool.SerialNumber;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsDao goodsDao;
    @Resource
    private MerchantDao merchantDao;

    /**
     * 新增商品接口
     */
    @Override
    public Result addGoods(Goods goods, JwtInfo jwtInfo){
        Result result =new Result();
        try {
            boolean pass = Arrays.stream(jwtInfo.getPurview()).anyMatch(s -> s.equals("admin")|| s.equals("merchant") || s.equals("superAdmin"));
            if (pass){
                Merchant merchant1 =merchantDao.selectById(jwtInfo.getUserId());
                if (merchant1!=null&&merchant1.getAccountStatus().equals("1")){
                   if (merchant1.getAuditStatus().equals("G")){
                       LambdaQueryWrapper<Goods> lqw =new LambdaQueryWrapper<>();
                       lqw.eq(Goods::getMerchantId,jwtInfo.getUserId());
                       int count = Math.toIntExact(goodsDao.selectCount(lqw));
                       //商家id
                       goods.setMerchantId(jwtInfo.getUserId());
                       //创建时间
                       goods.setCreateTime(new Date());
                       //待上架
                       goods.setStatus("D");
                       //商品id
                       goods.setGoodsId(SerialNumber(count+1));
                      int res =goodsDao.insert(goods);
                      if (res==1){
                          result.setCode(ResultEnum.SUCCESS.getCode());
                          result.setMsg("商品添加成功");
                      }else {
                          result.setCode(ResultEnum.INVALID.getCode());
                          result.setMsg("商品添加失败");
                      }
                   }else {
                       result.setCode(ResultEnum.FAILED_VISA_INSPECTION.getCode());
                       result.setMsg("账户异常拒绝操作！");
                   }
                }else {
                    result.setCode(ResultEnum.FAILED_VISA_INSPECTION.getCode());
                    result.setMsg("账户异常拒绝操作！");
                }
            }else {
                result.setCode(ResultEnum.FAILED_VISA_INSPECTION.getCode());
                result.setMsg("您暂无权限！");
            }
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(e);
        }
        return result;
    }
}
